<!--
+----------------------------------------------------------------------
| 友得云客  - 开启房产营销新纪元
+----------------------------------------------------------------------
| Copyright (c) 2019~2023 优得（西安）信息科技有限公司版权所有
+----------------------------------------------------------------------
| Licensed 友得云客不是自由软件 未经允许不可移除相关版权
+----------------------------------------------------------------------
| Author: www.youdeyunke.com
+----------------------------------------------------------------------
-->
<template>
    <el-dialog :visible.sync="show" width="600px" :destroy-on-close="true" :close-on-click-modal="false">
        <el-form size="small" label-position="right" label-width="120px" v-loading="loading">

            <el-form-item label="标题">
                <el-input :minlength="4" v-model="currentItem.title" placeholder="请输入资讯标题"></el-input>
            </el-form-item>

            <el-form-item label="内容">
                <el-input type="textarea" :minlength="10" :rows="5" show-word-limit v-model="currentItem.content"
                    placeholder="请输入资讯内容"></el-input>
            </el-form-item>

            <el-form-item label="阅读人数">
                <el-input type="Number" v-model="currentItem.view_nums" placeholder="请输入阅读人数"
                    style="width:220px;"></el-input>
            </el-form-item>

            <el-form-item label="分享人数">
                <el-input type="Number" v-model="currentItem.share_nums" placeholder="请输入分享人数"
                    style="width:220px;"></el-input>
            </el-form-item>

            <el-form-item label="发布时间" label-position="left">
                <el-date-picker style="width: 220px" v-model="currentItem.date" type="datetime" placeholder="选择日期" 
                    value-format="yyyy-MM-dd HH:mm:ss">
                </el-date-picker>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button size="mini" type="default" icon="el-icon-close" @click="cancleHandle" :disabled="loading">取消</el-button>
            <el-button size="mini" type="primary" icon="el-icon-check" @click="submitHandle" :disabled="loading">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import PostSelector from "@/components/PostSelector";
import {
    createBulletin,
    updateBulletin,
} from "@/api/bulletin";
export default {
    name: "itemForm",
    components: { PostSelector, },

    EnumerationSelectorprops: {},
    data() {
        return {
            currentItem: {},
            show: false,
            loading: false,
        };
    },
    computed: {
        canSubmit: function () {
            var h = this.currentItem;
            if (h.desc && h.name && h.expired_at) {
                return true;
            }
            return false;
        },
    },
    watch: {},
    methods: {
        openDialog: function (item) {
            this.currentItem = item;
            this.show = true;
        },

        cancleHandle: function () {
            this.show = false;
            this.currentItem = {};
        },
        submitHandle: function () {
            var isok = this.validate(this.currentItem);
            if (!isok) {
                return;
            }
            var isNew = !this.currentItem.id;
            if (isNew) {
                return this.doCreate();
            }
            return this.doUpdate();
        },

        doCreate: function () {
            this.loading = true;
            createBulletin(this.currentItem).then((resp) => {
                this.loading = false;
                if (resp.status == 0) {
                    this.$message.success("创建成功");
                    this.loading = false;
                    this.show = false;
                    this.$emit("change", {});
                }
            });
        },

        doUpdate: function () {
            var _this = this;
            this.loading = true;
            updateBulletin(this.currentItem).then((resp) => {
                this.loading = false;
                if (resp.status == 0) {
                    _this.$message.success("保存成功");
                    _this.$emit("change", {});
                    this.show = false;
                    _this.loading = false;
                }
            });
        },

        validate: function (data) {

            if (!data.title) {
                this.$message.error("请检查标题");
                return false
            }
            if (!data.content) {
                this.$message.error("请检查资讯");
                return false
            }
            if (!data.date) {
                this.$message.error("请检查时间");
                return false
            }
            if (!data.view_nums || data.view_nums < 0) {
                this.$message.error("请检查阅读人数");
                return false
            }
            if (data.share_nums < 0) {
                this.$message.error("请检查分享人数");
                return false
            }

            // TODO
            return true;
        },
    },
};
</script>

<style scoped>
.tips ul {
    padding: 0;
    margin: 0;
}

.tips {
    font-size: 12px;
    color: #999;
}

.show-more {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    margin-top: 10px;
}

.icon-uploader {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 35px;
    width: 100%;
    min-height: 100px;
    border: 2px dashed #f4f4f4;
}

.icon-uploader .el-upload {
    width: 100%;
    height: 100%;
}
</style>
